//
//  HMTeacgerEvaluateDetailVC.m
//  BestLearning
//
//  Created by 陈辉明 on 2017/12/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HMTeacgerEvaluateDetailVC.h"
#import "HMTeacgerEvaluateDetailOneCell.h"
#import "HMTeacgerEvaluateDetailGoodCell.h"
#import "HNTeacgerEvaluateDetailModel.h"
#import "HMTeacgerEvaluateDetailHeadView.h"
#import "HMChildrenShowCell.h"
#import "HMUniversalityProblemCellTableViewCell.h"
#import "HMIndividualityCell.h"
#import "ReceiveFlowerView.h"


@interface HMTeacgerEvaluateDetailVC () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) HNTeacgerEvaluateDetailModel *model;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) NSMutableArray *dataSourceTow;
@property(nonatomic,strong) NSMutableArray *dataSourceThree;
@property(nonatomic,strong) NSMutableArray *dataSourceFour;
@property(nonatomic,strong) HMTeacgerEvaluateDetailHeadView *headView;
@property (weak, nonatomic) IBOutlet UIView *faildView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activity;





@end

@implementation HMTeacgerEvaluateDetailVC


- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSourceTow = [NSMutableArray array];
    self.dataSourceThree = [NSMutableArray array];
    self.dataSourceFour  = [NSMutableArray array];
     [self setNetWork];
    [self setUpTableView];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(leftBarButtonItemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
    self.title = @"评论详情";

   
}

- (void)leftBarButtonItemAction{
    
    
    if (self.isPush) {
        if (self.popBlock) {
            self.popBlock();
        }
        
    }
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)setUpTableView {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.estimatedRowHeight = 60;
    self.tableView.rowHeight = UITableViewAutomaticDimension;    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"HMTeacgerEvaluateDetailOneCell" bundle:nil] forCellReuseIdentifier:@"HMTeacgerEvaluateDetailOneCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HMTeacgerEvaluateDetailGoodCell" bundle:nil] forCellReuseIdentifier:@"HMTeacgerEvaluateDetailGoodCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HMChildrenShowCell" bundle:nil] forCellReuseIdentifier:@"HMChildrenShowCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HMChildrenShowCell" bundle:nil] forCellReuseIdentifier:@"HMChildrenShowCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HMUniversalityProblemCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"HMUniversalityProblemCellTableViewCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HMIndividualityCell" bundle:nil] forCellReuseIdentifier:@"HMIndividualityCellId"];
    
    
    
}

#pragma mark - tableViewDataSource,tableViewDelegate


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return 1 + self.dataSource.count + 1 +1 +1;
    return 7;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (0 == section) {
            return 1;

    }
    else if(1 == section) {
            return self.dataSourceTow.count;
    }
    else if (2== section) {
            return self.dataSourceThree.count;
    }
    else if (3 == section){
            return self.dataSourceFour.count;
    
    }
    else if (4 == section){
        return 1;
    }
    else if (5 == section){
        return 1;
    }
    else if (6 == section){
        return 1;
    }
    
    else {
        return 0;
    }


}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HMTeacgerEvaluateDetailOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMTeacgerEvaluateDetailOneCellId" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.model) {
            cell.model = self.model;
            cell.lessonName = self.SubjectTitle;
        }
        return cell;
    }else if (indexPath.section == 1) {
        NSDictionary *dict = self.dataSourceTow[indexPath.row];
        HMTeacgerEvaluateDetailGoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMTeacgerEvaluateDetailGoodCellId" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.oneDict = dict;
        return cell;
    }else if (indexPath.section == 2) {
        NSDictionary *dict = self.dataSourceThree[indexPath.row];
        HMTeacgerEvaluateDetailGoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMTeacgerEvaluateDetailGoodCellId" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.oneDict = dict;
        return cell;
    }else if (indexPath.section == 3) {
        NSDictionary *dict = self.dataSourceFour[indexPath.row];
        HMTeacgerEvaluateDetailGoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMTeacgerEvaluateDetailGoodCellId" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.oneDict = dict;
        return cell;
    }else if (indexPath.section == 4) {
        
        HMChildrenShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMChildrenShowCellId" forIndexPath:indexPath];
//        HMChildrenShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMChildrenShowCellId"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model =self.model;
        return cell;
    }else if (indexPath.section == 5) {
        HMUniversalityProblemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMUniversalityProblemCellTableViewCellId" forIndexPath:indexPath];
        cell.model = self.model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 6) {
        HMIndividualityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMIndividualityCellId" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model;
        return cell;
    }
    
    else {
        return  nil;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if ((section == 1 &&self.dataSourceTow.count > 0)|| (section == 2&&self.dataSourceThree.count > 0) || (section == 3&&self.dataSourceFour.count > 0)) {
        HMTeacgerEvaluateDetailHeadView  *headView = [[NSBundle mainBundle] loadNibNamed:[HMTeacgerEvaluateDetailHeadView description] owner:nil options:nil][0];
        if (section == 1 && self.dataSourceTow.count > 0) {
            headView.titleLabel.text = @"好习惯";
        }
        if (section == 2 && self.dataSourceThree.count > 0) {
            headView.titleLabel.text = @"好成绩";
        }
        if (section == 3&&self.dataSourceFour.count > 0) {
            headView.titleLabel.text = @"好素养";
        }
        return headView;
    } else {
        return nil;
    }

}
//
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ((section == 1 &&self.dataSourceTow.count > 0)|| (section == 2&&self.dataSourceThree.count > 0) || (section == 3&&self.dataSourceFour.count > 0)) {
        return 51.0f;
    } else {
        return 0.0f;
    }
}





#pragma mark - 网络请求
- (void)setNetWork
{
    [MBProgressHUD showHUD];
    
    // NSString *time = [LLTools mgrTimeYY_MM_TT:self.time];
    
    //http://192.168.0.113:8080/StarShine/ws/aflatunLesson.do?action=getCommentDetailById2&id=5&isRead=1
    NSString *UrlStr = [NSString stringWithFormat:@"aflatunLesson.do?action=getCommentDetailById2&id=%@&isRead=%@",self.ID,self.time];
    
    NSLog(@"%@ %@",LLBaseURLs(UrlStr),[AccountTool shareAccountTool].currentAccount.authCode);
    [NetManager Get:LLBaseURLs(UrlStr) Auth:YES Params:nil Success:^(id Obj) {
        
        [self.activity stopAnimating];
        if ([Obj[@"code"] isEqualToNumber:@1000])
        {
            self.faildView.hidden = YES;
            [MBProgressHUD hideHUD];
            [self.dataSource removeAllObjects];
            [self.dataSourceTow removeAllObjects];
            [self.dataSourceThree removeAllObjects];
            [self.dataSourceFour removeAllObjects];
            self.model = nil;
            
            NSDictionary *t  = [Obj valueForKey:@"t"];
            NSArray *slist = [t valueForKey:@"slist"];
            
            for (NSDictionary *dict  in slist) {
                
                //好习惯
                if ([dict[@"type"] isEqualToNumber:@1])
                {
                    
                    [self.dataSourceTow addObject:dict];
                }
                else if ([dict[@"type"] isEqualToNumber:@2])
                {//好成绩
                    [self.dataSourceThree addObject:dict];
                }
                else if ([dict[@"type"] isEqualToNumber:@3])
                {//好素养
                    [self.dataSourceFour addObject:dict];
                }
                
                [self.dataSource addObject:dict];
                
                
                
            }
            
            self.model = [[HNTeacgerEvaluateDetailModel alloc] init_HNObjectForKeys:Obj[@"t"]];
            [self.tableView reloadData];
            
            //            // 奖励红花的弹窗
            NSString *str = Obj[@"t"][@"param"];
            if (str.length > 0 && ![str isKindOfClass:[NSNull class]]) {
                
                [ReceiveFlowerView receiveFlowerWithMsg:str Complete:nil];
            }
        }
        else
        {
            [MBProgressHUD showError:Obj[@"msg"]];
            self.faildView.hidden = NO;
            
        }
        
    } Fail:^(NSError *Error) {
        [self.activity stopAnimating];
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NetErr];
        self.faildView.hidden = NO;
    }];
    
    
    
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];

    [SVProgressHUD dismiss];
}





- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];
}


@end
